package DAO;

import java.awt.List;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;


public abstract class DAOArquivo<T> implements DAOInterface<T>{

	File hotel = new File("C:/Arquivos/hotel.txt");

	public void salvar(String arquivo, T obj)
			throws IOException {
		try {
			File file = new File(arquivo);
			FileOutputStream f = new FileOutputStream(file);
			ObjectOutputStream o = new ObjectOutputStream(f);
			o.writeObject(obj);
			f.close();
		} catch(IOException e){
			System.err.println(e.getMessage());
		}
	}

	public T procurar(String arquivo, T obj)
		throws IOException, FileNotFoundException, ClassNotFoundException{
		try {
			File file = new File(arquivo);
		    FileInputStream fis = new FileInputStream(file);
		    ObjectInputStream reader = new ObjectInputStream(fis);
		    T registro = (T) reader.readObject();
		    registro[i].print(); 
    	    reader.close();  
    	    fis.close();  
    	}  
		    
	}

	public List<T> ler(String arquivo)
			throws IOException, ClassNotFoundException {
		File file = new File(arquivo);
		FileInputStream f = new FileInputStream(file);
		ObjectInputStream o = new ObjectInputStream(f);
		return (List<T>)o.readObject();
	}

	public static String carregar(String arquivo)throws FileNotFoundException, IOException {
		File file = new File(arquivo);
		if (! file.exists()) {
			return null;
		}

		BufferedReader br = new BufferedReader(new FileReader(arquivo));
		StringBuffer bufSaida = new StringBuffer();

		String linha;
		while( (linha = br.readLine()) != null ){
			bufSaida.append(linha + "/n");
		}
		br.close();
		return bufSaida.toString();
	}
}
